use salvo::prelude::*;
use crate::utils::download_file;

#[handler]
async fn handle_download(req: &mut Request, res: &mut Response) {
    if let Some(url) = req.query::<String>("url") {
        match download_file(&url).await {
            Ok(response) => *res = response,
            Err(e) => res.render(Text::Plain(format!("Download failed: {}", e))),
        }
    } else {
        res.render(Text::Plain("Missing url parameter"));
    }
}

pub fn router() -> Router {
    Router::with_path("download").get(handle_download)
}
